package com.dd.admin.business.sell.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.dd.admin.business.memberCard.entity.MemberCard;
import com.dd.admin.business.memberCard.service.MemberCardService;
import com.dd.admin.business.product.entity.Product;
import com.dd.admin.business.product.service.ProductService;
import com.dd.admin.business.sell.entity.Sell;
import com.dd.admin.business.sell.service.CancelSellService;
import com.dd.admin.business.sellDetail.domain.SellDetailDto;
import com.dd.admin.business.sellDetail.domain.SellDetailVo;
import com.dd.admin.business.sellDetail.entity.SellDetail;
import com.dd.admin.business.sellDetail.service.SellDetailService;
import com.dd.admin.business.sellPay.domain.SellpayVo;
import com.dd.admin.business.sellPay.entity.Sellpay;
import com.dd.admin.business.sellPay.service.SellpayService;
import com.dd.admin.business.stock.entity.Stock;
import com.dd.admin.business.stock.service.StockService;
import com.dd.admin.business.stockDetail.domain.StockDetailDto;
import com.dd.admin.business.stockDetail.entity.StockDetail;
import com.dd.admin.business.stockDetail.service.StockDetailService;
import com.dd.admin.common.exception.ApiException;
import com.dd.admin.common.security.SecurityUtil;
import com.dd.admin.common.utils.BigDecimalUtil;
import com.dd.admin.common.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.dd.admin.business.pay.enmus.CashierExceptionEnum.CARD_AMOUNT_CHANGE;
import static com.dd.admin.business.pay.enmus.PayConst.PAY_TYPE_CARD;
import static com.dd.admin.business.pay.enmus.PayConst.PAY_TYPE_CARD_GIVE;
import static com.dd.admin.common.consts.MemberShopConst.*;

@Service
@Slf4j
public class CancelMemberProduct implements CancelSellService {

    @Autowired
    MemberCardService memberCardService;
    @Autowired
    SellDetailService sellDetailService;
    @Autowired
    ProductService productService;
    @Autowired
    StockService stockService;
    @Autowired
    StockDetailService stockDetailService;
    @Autowired
    SellpayService sellpayService;

    @Override
    public void cancel(Sell sell) {
        List<SellDetail> sellDetailList = sellDetailService.selectSellDetailList(sell.getSellId());
        //增加库存
        addInventory(sellDetailList);

        MemberCard memberCard = memberCardService.getById(sell.getMemberCardId());

        if(memberCard!=null){
            BigDecimal cardAmount = memberCard.getAmount();
            log.info("当前卡余额:"+cardAmount);
            BigDecimal cardGiveAmount = memberCard.getGiveAmount();
            log.info("当前卡赠送金余额:"+cardAmount);


            List<Sellpay> sellPayVoList = sellpayService.selectSellPayList(sell.getSellId());

            //计算订单卡金支付的金额
            Sellpay memberCardPay = sellPayVoList.stream().filter(p->
                    p.getPayType().equals(PAY_TYPE_CARD)
            ).findFirst().orElse(null);
            Sellpay memberGivePay= sellPayVoList.stream().filter(p->
                    p.getPayType().equals(PAY_TYPE_CARD_GIVE)
            ).findFirst().orElse(null);

            if(memberCardPay!=null){
                memberCard.setAmount(BigDecimalUtil.add(cardAmount,memberCardPay.getAmount()));
            }
            if(memberGivePay!=null){
                memberCard.setGiveAmount(BigDecimalUtil.add(cardGiveAmount,memberGivePay.getAmount()));
            }
            Boolean b = memberCardService.updateById(memberCard);
            if(!b){
                throw new ApiException(CARD_AMOUNT_CHANGE);
            }
        }
    }


    void addInventory(List<SellDetail> productDetailList){

        List<StockDetail> stockDetailList = new ArrayList<>();
        //模拟入库
        productDetailList.stream().forEach(sellDetail -> {
            String productId = sellDetail.getProductId();
            Product product = productService.getById(productId);
            if(product.getStockControl().equals(STOCK_CONTROL_YES)){
                StockDetail stockDetail = new StockDetail();
                stockDetail.setProductId(product.getProductId());
                stockDetail.setProductName(product.getProductName());
                stockDetail.setCount(sellDetail.getCount());
                productService.addProductCount(product.getProductId(),sellDetail.getCount());
                stockDetailList.add(stockDetail);
            }
        });


        if(CollectionUtil.isNotEmpty(stockDetailList)){
            //创建入库总单
            Stock stock = new Stock();
            stock.setStockInType(STOCK_IN_TYPE_CANCEL);
            stock.setStockTitle(DateUtil.formatChineseDate(new Date(),false,true) + SecurityUtil.getLoginUser().getDeptName() +  "入库单");
            stock.setStockNo(StringUtil.getDateStringNow() + StringUtil.createCode(4));
            stock.setStockType(STOCK_IN);
            stockService.save(stock);

            stockDetailList.stream().forEach(stockDetail -> {stockDetail.setStockId(stock.getStockId());});
            stockDetailService.saveBatch(stockDetailList);
        }
    }
}
